package com.idea.plugin.core;

import com.idea.plugin.core.constant.CommonConstant;
import com.idea.plugin.core.domain.dto.JavaClassDTO;
import com.idea.plugin.core.domain.info.JavaClassInfo;
import com.idea.plugin.core.helper.FMUtils;
import com.idea.plugin.core.resolver.UnitTestingClassResolver;
import freemarker.template.TemplateException;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author yanzhao
 * @date 2022/2/22 11:31
 * @since 1.0.0
 */
public class Generator {

    public static void main(String[] args) throws Exception {
        genTest();
    }

    public static void genTest() throws IOException, TemplateException {
        String baseDir = "/Users/yangzhen/code/edu-springboot-powermock";
        String absolutePath = "/Users/yangzhen/code/edu-springboot-powermock/src/main/java/com/example/edu/service/DependencyService.java";

        CommonConstant.CONFIG_ENTITY.setBasedir(new File(baseDir));
        final JavaClassInfo javaClassInfo = new JavaClassInfo();
        javaClassInfo.setBaseDir(baseDir);
        javaClassInfo.setAbsolutePath(absolutePath);
        JavaClassDTO javaClassDTO = UnitTestingClassResolver.build(javaClassInfo);
        //获取类中方法
        if (javaClassDTO == null) {
            return;
        }
        File testFile = new File(javaClassInfo.getTestAbsolutePath());

        Map<String, Object> data = new HashMap<>(2);
        data.put("javaClassDTO", javaClassDTO);

        //设置配置文件 freemarker
        //获取mock的类
        //文件不存在,进行初始化生成
        FMUtils.writeFromClasspath("unit-testing.ftl", testFile, data);
    }
}
